package com.xizimall.service.impl;

import java.util.Date;
import java.util.List;

import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xizimall.mapper.TbItemDescMapper;
import com.xizimall.mapper.TbItemMapper;
import com.xizimall.mapper.TbItemParamItemMapper;
import com.xizimall.pojo.TbItem;
import com.xizimall.pojo.TbItemDesc;
import com.xizimall.pojo.TbItemExample;
import com.xizimall.pojo.TbItemExample.Criteria;
import com.xizimall.pojo.TbItemParamItem;
import com.xizimall.service.ItemService;
import com.xizimall.common.pojo.EUDataGridResult;
import com.xizimall.common.pojo.XizimallResult;
import com.xizimall.common.utils.IDUtils;

/**
 * 商品管理Service
 * <p>Title: ItemServiceImpl</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2015年9月2日上午10:47:14
 * @version 1.0
 */
@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;
	@Override
	public TbItem getItemById(long itemId) {
		
		//TbItem item = itemMapper.selectByPrimaryKey(itemId);
		//添加查询条件
		TbItemExample example = new TbItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(itemId);
		List<TbItem> list = itemMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			TbItem item = list.get(0);
			return item;
		}
		return null;
	}
/**
 * 商品列表查询
 * <p>Title: getItemList</p>
 * <p>Description: </p>
 * @param page
 * @param rows
 * @return
 * @see com.xizimall.service.ItemService#getItemList(int, int)
 */
	@Override
	public EUDataGridResult getItemList(int page, int rows) {
		//查询商品列表
		TbItemExample example=new TbItemExample();
		//分页处理
		PageHelper.startPage(page, rows);
		List<TbItem> list=itemMapper.selectByExample(example);
		//创建返回对象
		EUDataGridResult result=new EUDataGridResult();
		result.setRows(list);
		//获取记录总数
		PageInfo<TbItem> pageInfo=new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
		
	}
@Override
public XizimallResult createItem(TbItem item,String desc,String itemParam) throws Exception{
	// Item补全
	//生成商品id
	long itemId =IDUtils.genItemId();
	item.setId(itemId);
	//商品状态 1正常 2下架 3删除
	item.setStatus((byte)1);
	item.setCreated(new Date());
	item.setUpdated(new Date());
	//插入数据库
	itemMapper.insert(item);
	//添加商品描述
	XizimallResult result =insertItemDesc(itemId, desc);
	if(result.getStatus()!=200){
		throw new Exception();
	}
	//添加规格参数
  result =insertItemParamItem(itemId, itemParam);
  if(result.getStatus()!=200){
	  throw new Exception();
  }
	return XizimallResult.ok();
}
   /**
    * 添加商品描述
    * <p>Title: insertItemDesc</p>
    * <p>Description: </p>
    * @param itemId
    * @param desc
    * @return
    */
   private XizimallResult insertItemDesc(Long itemId,String desc){
	   TbItemDesc itemDesc=new TbItemDesc();
	   itemDesc.setItemId(itemId);
	   itemDesc.setItemDesc(desc);
	   itemDesc.setCreated(new Date());
	   itemDesc.setUpdated(new Date());
	   itemDescMapper.insert(itemDesc);
	   return XizimallResult.ok();
   }
   private XizimallResult insertItemParamItem(Long itemId,String itemParam){
	   TbItemParamItem itemParamItem=new TbItemParamItem();
	   itemParamItem.setItemId(itemId);
	   itemParamItem.setParamData(itemParam);
	   itemParamItem.setCreated(new Date());
	   itemParamItem.setUpdated(new Date());
	   itemParamItemMapper.insert(itemParamItem);
	   return XizimallResult.ok();
	   
   }
   
}